package com.monkey.luffer.springboot.demo.basis.kcc.s3cmd;

import org.apache.commons.cli.*;
import org.apache.commons.lang3.StringUtils;


/**
 * @author jiahong.liu
 */
public class MyCmdlineHelper {

	Options options;
	CommandLineParser commandLineParser;

	public MyCmdlineHelper(){
		String helpString = "args: --bucket --prefix --command --dstPath --srcPath --version --env --ticketFlag --gitFlag --preVersion --curVersion";
		options = new Options();
		options.addOption("help", helpString);
		options.addOption(Option.builder("b").argName("bucket").longOpt("bucket").hasArg().required(false).type(Boolean.class).build());
		options.addOption(Option.builder("p").argName("prefix").longOpt("prefix").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("c").argName("command").longOpt("command").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("d").argName("dstPath").longOpt("dstPath").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("s").argName("srcPath").longOpt("srcPath").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("v").argName("version").longOpt("version").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("e").argName("env").longOpt("env").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("t").argName("ticketFlag").longOpt("ticketFlag").hasArg().required(false).type(Boolean.class).build());
		options.addOption(Option.builder("g").argName("gitFlag").longOpt("gitFlag").hasArg().required(false).type(Boolean.class).build());
		options.addOption(Option.builder("pre").argName("preVersion").longOpt("preVersion").hasArg().required(false).type(String.class).build());
		options.addOption(Option.builder("cur").argName("curVersion").longOpt("curVersion").hasArg().required(false).type(String.class).build());
		commandLineParser = new DefaultParser();
	}

	public MyCmd parse(String[] param) throws ParseException {
		try{
			CommandLine commandLine = commandLineParser.parse(options, param);
			MyCmd myCmd = MyCmd.builder().build();
			myCmd.setBucket(commandLine.getOptionValue("b"));
			myCmd.setPrefix(commandLine.getOptionValue("p"));
			myCmd.setCommand(commandLine.getOptionValue("c"));
			myCmd.setDstPath(StringUtils.isNotBlank(commandLine.getOptionValue("d"))? commandLine.getOptionValue("d") : ".");
			myCmd.setSrcPath(StringUtils.isNotBlank(commandLine.getOptionValue("s"))? commandLine.getOptionValue("s") : ".");
			myCmd.setVersion(commandLine.getOptionValue("v"));
			myCmd.setEnv(commandLine.getOptionValue("e"));
			myCmd.setTicketFlag(StringUtils.isNotBlank(commandLine.getOptionValue("t")) && Boolean.parseBoolean(commandLine.getOptionValue("t")));
			myCmd.setGitFlag(StringUtils.isBlank(commandLine.getOptionValue("g")) || Boolean.parseBoolean(commandLine.getOptionValue("g")));
			myCmd.setPreVersion(commandLine.getOptionValue("pre"));
			myCmd.setCurVersion(commandLine.getOptionValue("cur"));
			return myCmd;
		} catch (ParseException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
